package com.itany.corejava.day13;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.math.MathContext;

public class Calculator {

    //定义组件
    private JFrame frame;
    private JTextField field;
    private JButton clearButton;
    private JButton[] allButton;

    private String os;
    private int fi=-1;

    public static final int ADD=0;
    public static final int SUBTRACT=1;
    public static final int MULTIPLY=2;
    public static final int DIVIDE=3;

    //创建组件
    public Calculator() {
        frame=new JFrame("计算器");
        field=new JTextField("0",19);
        clearButton=new JButton("C");
        String s="123+456-789*0.=/";
        allButton=new JButton[s.length()];
        for (int i = 0; i < allButton.length; i++) {
            allButton[i]=new JButton(s.substring(i,i+1));
        }
    }

    //布局
    private void init(){
        frame.setLayout(new BorderLayout(3,3));

        JPanel northPanel=new JPanel();
        JPanel centerPanel=new JPanel();

        northPanel.setLayout(new FlowLayout());
        centerPanel.setLayout(new GridLayout(4,4,6,6));


        northPanel.add(field);
        northPanel.add(clearButton);

        for (int i = 0; i < allButton.length; i++) {
            centerPanel.add(allButton[i]);
        }

        frame.add(northPanel,BorderLayout.NORTH);
        frame.add(centerPanel,BorderLayout.CENTER);


    }

    //设置样式
    private void setStyle(){
        clearButton.setFont(new Font("Courier New",Font.BOLD+Font.ITALIC,16));


        Font font=new Font("Courier New",Font.PLAIN,16);
        for (int i = 0; i < allButton.length; i++) {
            allButton[i].setFont(font);
        }


        field.setBackground(Color.WHITE);
        field.setForeground(new Color(0,0,0));

        field.setHorizontalAlignment(JTextField.RIGHT);

        field.setEnabled(false);
    }

    //注册监听
    private void addEventHandler(){
        clearButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                os=null;
                fi=-1;
                field.setText("0");
            }
        });

        String n="0123456789.";
        String f="+-*/";

        for (int i = 0; i < allButton.length; i++) {
            String s=allButton[i].getActionCommand();
            if(n.indexOf(s)!=-1){
                //数字按钮
                allButton[i].addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("n");

                        if(f.indexOf(field.getText())!=-1){
                            field.setText("0");
                        }

                        if("0".equals(field.getText())&&!".".equals(s)){
                            field.setText("");
                        }

                        if(".".equals(s)&&!field.getText().matches("\\d+")){
                            return;
                        }

                        field.setText(field.getText()+s);
                    }
                });
            }
            if(f.indexOf(s)!=-1){
                //符号按钮
                allButton[i].addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("f");
                        if(fi==-1) {
                            os = field.getText();
                        }

                        field.setText(s);

                        fi=f.indexOf(s);
                    }
                });
            }
            if("=".equals(s)){
                //计算按钮
                allButton[i].addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("=");

                        try {
                            if(os==null||fi==-1||f.indexOf(field.getText())!=-1){
                                return;
                            }

                            String ns=field.getText();


                            System.out.println("os = " + os);
                            System.out.println("fi = " + fi);
                            System.out.println("ns = " + ns);


                            BigDecimal oi=new BigDecimal(os);
                            BigDecimal ni=new BigDecimal(ns);
                            BigDecimal result=null;

                            switch (fi){
                                case ADD:
                                    result=oi.add(ni);
                                    break;
                                case SUBTRACT:
                                    result=oi.subtract(ni);
                                    break;
                                case MULTIPLY:
                                    result=oi.multiply(ni);
                                    break;
                                case DIVIDE:
                                    result=oi.divide(ni, MathContext.DECIMAL32);
                                    break;
                            }
                            field.setText(result.doubleValue()+"");

                            fi=-1;
                        } catch (Exception e1) {
                            field.setText("错误");
                        }


                    }
                });
            }
        }
    }

    //显示窗口
    public void show(){
        init();
        setStyle();
        addEventHandler();
        frame.setSize(285,200);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    //运行
    public static void main(String[] args) {
        new Calculator().show();
    }
}
